package com.johannting.demo.job.ram;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;


public class RAMQuartz {
    private static Logger _log= LoggerFactory.getLogger(RAMQuartz.class);

     public static void main(String[] args) throws SchedulerException {
        //1.创建scheduler工厂
        SchedulerFactory sf=new StdSchedulerFactory();
        //2.从工厂获取实例
        Scheduler scheduler=sf.getScheduler();
        //3.创建jobDetail
        JobDetail jb= JobBuilder.newJob(RAMJob.class)
                .withDescription("this is a ram job") //job的描述
                .withIdentity("ramJob","ramGroup") //job的name与group
                .build();
        //任务运行的时间，SimpleSchedule类型触发器有效
        long time=System.currentTimeMillis()+7*1000L;  //延后7s，不一定严格按这7s执行
        Date startTime=new Date(time);
        //4.创建trigger
        Trigger t=TriggerBuilder.newTrigger()
                .withIdentity("ramTrigger","ramTriggerGroup")
                .withDescription("")
                .startAt(startTime)
                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
                .build();
        // cron表达式总会在60s内将周期跑完，例如"0/7 * * * * ?"，那么就是00s 07s 14s 21s 28s 35s 42s 49s 56s
        // 当startTime的时间间隔，也并不一定完整的按 延后的5s来，最终还是看cron在1min内的频次
        //5.注册任务和定时器
        scheduler.scheduleJob(jb,t);
        //6.启动 调度器
        scheduler.start();
        _log.info("启动时间："+new Date());

    }
}
